﻿<html>
<head>
<style>
.content{
	margin-top:100px;
	font-size:45px;
	background-color:#CCC;
}
</style>
</head>
<body>
	<div>
		<div id="hint">
			
		</div>
		<input type="button" id="btn1" value="获取数据"></input>
		<div class="content" id="content">
		
		</div>
		
	</div>
<script>
	if(!console){
		var console = {};
		console.debug = console.info = console.log = console.error = function(){return false;}
	}
	function setup(){
		/**
		* Ajax方法
		*/
		var Ajax = {
			//发出ajax请求
			request:function(cfg){
				if(cfg.url===undefined){return false;}//URL必填
				if(cfg.asyn===undefined){cfg.asyn=true}//默认异步请求
				var req = this.getRequest();
				var isPost = cfg.type.toUpperCase()=="POST";
				var p = encodeParams(cfg);
				req.open(cfg.type||"GET", cfg.url+(isPost||!cfg.hasParams ? "": "?"+p), cfg.asyn);
				if(isPost){req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");}
				req.send(isPost ? p : null);
				if(cfg.asyn){
					req.onreadystatechange = function(){
						if(this.readyState==4){
							cfg.success.call(req);
						}
					}
				}else{
					cfg.success.call(req);
				}
				
				//如果是POST，则将请求头加上如下内容
				
				function encodeParams(cfg){
					var r = [];
					for(var i in cfg.params){
						r.push(i+"="+cfg.params[i]);
					}
					cfg.hasParams = r,length>0;
					return r.join("&");
				}
			},
			//跨浏览器获取XMLHttpRequest
			getRequest: function(){
				var _x;
				if(typeof XMLHttpRequest=="function"){
					_x = new XMLHttpRequest();
				}else{
					try{ _x =  new ActiveXObject("Msxml2.XMLHTTP");}catch(e){
						try { _x = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {
							throw("XXX", "什么烂浏览器，Ajax也不支持，BS！");
						} 
					}
				}
				return _x;
			}
		}
		//
		function g(id){return document.getElementById(id);}
		
		//业务
		var size = 0;
		// Ajax.request({
			// url:"data.php",
			// type:"post",
			// params:{
				// size:size
			// }
		// });
		var btn = g("btn1"), ct = g("content");
		g("btn1").onclick = function(){
			Ajax.request({
				url:"data.php",
				type:"post",
				params:{
					size:size
				},
				success: function(){
					g("content").innerHTML += this.responseText;
					size += 10;
				}
			});
		}
		window.x = ct;
		window.onscroll = function(e){
			
			//console.debug(content.clientHeight, content.offsetLeft, content.offsetTop, e);
			/*Ajax.request({
				url:"data.php",
				type:"post",
				params:{
					size:size
				}
			});*/
		}
		// Ajax.request({
			// url:"http://www.sohu.com",
			// type:"GET",
			// success:function(req){
				// alert(1);
				// alert(req);
			// }
		// })
		
	}
	window.onload = setup;
	
</script>
</body>
</html>